package com.example.month11.fragment2.fragments;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.month11.fragment2.base.BaseFragment;
import com.example.recyclerview01.R;

/**
 * 主界面底部的
 * Activity与Fragment通信的正确写法。拿到fragment.xxx();
 * Fragment与Activity通信的正确写法。而是fragment对外提供自己的事件回调，activity自己选择是否监听。
 */
public class MainMenuFragment extends BaseFragment implements View.OnClickListener {
    public static final String TAG = "MainMenuFragment";
    private Callback mCallback;

    public static MainMenuFragment newInstance() {
        return new MainMenuFragment();
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (!(context instanceof Callback)) {
            throw new IllegalArgumentException("The container activity should implement the " + TAG + "#Callback.");
        }
        mCallback = (Callback) context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main_menu, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        view.findViewById(R.id.btn_page01).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_page01: // 实时预览
                mCallback.showPage01(this);
                break;
        }
    }

    @Override
    public boolean onFragmentBackPressed() {
        super.onActivityBackPressed();
        return true;
    }

    public interface Callback {
        // 页面1显示。
        void showPage01(BaseFragment fragment);
    }

}
